﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MedicalLog.Domain.Entities;
using System.Net.Mail;

namespace MedicalLog.Domain.Repositories
{
    public class DoctorRepository
    {

        MedicalLogEntities _dbMedicalLog;
        public DoctorRepository()
        {
            _dbMedicalLog = new MedicalLogEntities();
        }


        
        public void AddDoctor(Doctor newDoctor)
        {
            _dbMedicalLog.AddToDoctors(newDoctor);
            _dbMedicalLog.SaveChanges();
        }


        public Doctor DeleteDoctor(int id)
        {
            try
            {
                return _dbMedicalLog.Doctors.First(doctor => doctor.IdDoctor == id);
            }
            catch (Exception)
            {

                throw;
            }

        }


        public void DeleteDoctor(Doctor doctor)
        {
            _dbMedicalLog.Doctors.DeleteObject(_dbMedicalLog.Doctors.Where(x => x.IdDoctor == doctor.IdDoctor).First());
            _dbMedicalLog.SaveChanges();
        }


        public Doctor UpdateDoctor(int id)
        {
            try
            {
                Doctor doc = _dbMedicalLog.Doctors.First(doctor => doctor.IdDoctor == id);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }

        }

        public void UpdateDoctor(Doctor doctor)
        {
            _dbMedicalLog.Doctors.First(d => d.IdDoctor == doctor.IdDoctor);
            _dbMedicalLog.Doctors.ApplyCurrentValues(doctor);
            _dbMedicalLog.SaveChanges();
        }


        public Doctor DetailDoctor(int id)
        {
            try
            {
                return _dbMedicalLog.Doctors.First(doctor => doctor.IdDoctor == id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IQueryable<Doctor> getDoctors()
        {
            return _dbMedicalLog.Doctors.Select(x => x);
        }

        public bool Login(string pUserName, string pPassword)
        {
            var Doctor = (from d in _dbMedicalLog.Doctors
                          where d.Username == pUserName && d.Passsword == pPassword
                          select d);

            if (Doctor.Any())
            {
                return true;

            }

            return false;
        }

        public void RecoverPassword(string pToMailAddress)
        {
            MailMessage Message = new MailMessage();

            var Result = (from p in _dbMedicalLog.Doctors
                          where p.Email == pToMailAddress
                          select p.Passsword);

            Message.From = new MailAddress("josealbertodeleon8@gmail.com");
            Message.To.Add(pToMailAddress);
            Message.Body ="Su Contraseña es: "+ Result.First();
            Message.Subject = ("MedicalLog - Password Recovery");
            Message.IsBodyHtml = false;


            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new System.Net.NetworkCredential("josealbertodeleon8@gmail.com", "Compaq123456");
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.Send(Message);

                  
        }

    }
}
